/*
 * @Author: chenli chenli@kcwl.com
 * @Date: 2023-07-14 14:47:18
 * @LastEditors: chenli chenli@kcwl.com
 * @LastEditTime: 2023-07-21 08:21:34
 * @FilePath: \woshiziyuande\decorator\methodDecorator.ts
 * @Description: 方法装饰器
 */
//普通方法装饰器
const metDec: MethodDecorator = (
  target,
  propertyKey,
  descriptor: PropertyDescriptor
) => {
  console.log("在方法装饰器里练习两年半");
  descriptor.value = (val1: string, val2: string) => {
    console.log(`基尼太美`, val1, val2);
  };
};

//静态方法装饰器
const staticMetDec: MethodDecorator = (
  target,
  propertyKey,
  descriptor: PropertyDescriptor
) => {
  console.log("在静态方法装饰器里练习后");
  descriptor.value = (val1: string, val2: string) => {
    console.log(`退圈了呀`, val1, val2);
  };
};

/**
 * 防抖
 * @param time 延迟时间
 * @returns 方法装饰器
 */
interface Year {
  time: number;
  msg: string;
}
const TimeDec = (data: Year,age:number): MethodDecorator => {
  return (target, propertyKey, descriptor: PropertyDescriptor) => {
    let oldFun = descriptor.value;
    let timeLimit:NodeJS.Timeout;
    descriptor.value = function (...args: []) {
      if (timeLimit) {
        console.log(data.msg,...args,age);
        clearTimeout(timeLimit);
      }
      timeLimit = setTimeout(() => {
        oldFun.apply(this, args);
      }, data.time * 1000);
    };
  };
};
class Kun {
  constructor() {}

  public caiyi(val1: string, val2: string) {
    console.log("练习前", val1, val2);
  }

  @metDec
  public caiyi2year(val1: string, val2: string) {
    console.log("练习2年后", val1, val2);
  }

  public static caiyi3year(val1: string, val2: string) {
    console.log("练习3年后", val1, val2);
  }

  @staticMetDec
  public static caiyi4year(val1: string, val2: string) {
    console.log("练习4年后", val1, val2);
  }

  @TimeDec({
    time: 2,
    msg:'练习太快,马上去踩缝纫机了'
  },18)
  public caiyi5year(val1: string, val2: string) {
    console.log("练习5年后", val1, val2);
  }
}

let kun = new Kun();
kun.caiyi("篮球", "Rap");
kun.caiyi2year("篮球", "Rap");
Kun.caiyi3year("篮球", "Rap");
Kun.caiyi4year("篮球", "Rap");
kun.caiyi5year("篮球5", "Rap5");
kun.caiyi5year("篮球55", "Rap55");
kun.caiyi5year("篮球555", "Rap555");
kun.caiyi5year("篮球5555", "Rap5555");
kun.caiyi5year("篮球55555", "Rap55555");
